/**
 * 
 */
package mapreduce4j;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;
import org.apache.log4j.Logger;

/**
 * Simplest of simple
 * @author tim
 */
public class FileRecordWriter<KEY_OUT extends WritableComparable> extends RecordWriter {
	protected Logger logger = Logger.getLogger(this.getClass().getName());
	protected DataOutputStream output;
	
	public FileRecordWriter(String s) throws IOException {
		File f = new File(s);
		if (!f.exists()) {
			f.createNewFile();
		}
		output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
	}
	
	/**
	 * closes the file
	 */
	@Override
	public void close() throws IOException, InterruptedException {
		output.flush();
		output.close();
	}

	/**
	 * writes to the file 
	 */
	@Override
	public void write(WritableComparable key, WritableComparable value) throws IOException,InterruptedException {
		key.write(output);
		value.write(output);
	}
}
